package com.lipuwater.agent.ui.fragment;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;

import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseFragment;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.ui.activity.MainActivity;
import com.lipuwater.agent.ui.event.KeyDownEvent;
import com.lipuwater.agent.ui.widget.JSBridgeWebView;
import com.lipuwater.agent.utils.jsbridge.JsBridgeListener;
import com.lipuwater.agent.utils.jsbridge.JsBridgeType;
import com.umeng.UMUtils;
import com.video.JCVideoPlayer;
import com.video.JCVideoPlayerStandard;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/3/13.
 * Email:924686754@qq.com
 * 培训
 */
public class FragmentTraining extends BaseFragment implements JsBridgeListener {

    @Bind(R.id.jsbridge_webciew)
    JSBridgeWebView jsbridgeWebciew;
    private View view;

    private String shareTitle, shareContent, shareLogo, shareUrl;
    private MainActivity mMainActivity = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.webview, null);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        init();
    }

    private void init() {
        mMainActivity = ((MainActivity) getActivity());
        mContentView.addView(view);
        jsbridgeWebciew.setJsBridge(this);
        jsbridgeWebciew.loadUrl(HttpConfig.TRAINING);
        mMainActivity.layoutLeftBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!jsbridgeWebciew.canGoBack()) {
                    mMainActivity.exit();
                } else {
                    jsbridgeWebciew.goBack();
                }
            }
        });
        // jsbridgeWebciew.loadUrl("http://wdh1025.ae124.zuji-08.com/demo.html");
    }

    @Override
    public void results(final JsBridgeType JsType, final Object object) {
        try {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //显示和隐藏返回的view
                    if (!jsbridgeWebciew.canGoBack()) {
                        //webview回到根节点了
                        mMainActivity.layoutLeftBack.setVisibility(View.GONE);
                        mMainActivity.mVisibleBack = false;
                    } else {
                        if (!mMainActivity.mVisibleBack) {
                            mMainActivity.layoutLeftBack.setVisibility(View.VISIBLE);
                            mMainActivity.mVisibleBack = true;
                        }
                    }
                    //下面处理js桥逻辑
                    final Map<String, Object> map = (Map<String, Object>) object;
                    if (JsType == JsBridgeType.VIDEO) {
                        JCVideoPlayerStandard.startFullscreen(getActivity(), JCVideoPlayerStandard.class, map.get("url").toString(), map.get("title").toString());
                    } else if (JsType == JsBridgeType.OPENSHARE) {
                        if ((boolean) (map.get("share"))) {
                            ((MainActivity) getActivity()).mVisibleShare = true;
                            ((MainActivity) getActivity()).layoutRight.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    UMUtils.share(getActivity(), shareTitle, shareContent, shareUrl + "?memberId=" + id, shareLogo);
                                }
                            });
                            if (((MainActivity) getActivity()).position == 1) {
                                ((MainActivity) getActivity()).layoutRight.setVisibility(View.VISIBLE);
                            }
                        } else {
                            ((MainActivity) getActivity()).mVisibleShare = false;
                            ((MainActivity) getActivity()).layoutRight.setVisibility(View.GONE);
                        }
                    } else if (JsType == JsBridgeType.SHARE) {
                        shareTitle = map.get("title").toString();
                        shareContent = map.get("content").toString();
                        shareLogo = map.get("logo").toString();
                        shareUrl = map.get("url").toString();
                    }
                }
            });
        } catch (Exception e) {
        }
    }


    public void onEventMainThread(KeyDownEvent event) {
        if (event.getPosition() == 1) {
            if (JCVideoPlayer.backPress()) {
                //如果在播放视频就直接return
                return;
            }
            if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && !jsbridgeWebciew.canGoBack()) {
                ((MainActivity) getActivity()).exit();
            } else {
                jsbridgeWebciew.goBack();
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        jsbridgeWebciew.destroyWebView();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }


}
